package com.luck.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import com.luck.dto.OrderItemDao;
import com.luck.dto.OrderMasterDao;
import com.luck.dto.OrderingDTO;
import com.luck.dto.OrderingOk;
import com.luck.feignClient.StockService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Slf4j
@Service
public class OrderService {
    @Autowired
    private OrderItemDao orderItemDao;
    @Autowired
    private OrderMasterDao orderMasterDao;
    @Autowired
    private StockService stockService;
    @GlobalTransactional(name="createOrder-lk")
    public OrderingOk order(OrderingDTO dto){
        dto.setId(Long.valueOf(1888888888L));
        dto.getProducts().forEach(p->{
            p.setOrderId(dto.getId());
        });
        String freeze = freeze(dto);
        log.debug("freeze:"+freeze);
        order2(dto);
        return new OrderingOk().setId(100L).setNeedPay(new BigDecimal(100));

    }
    public String freeze(OrderingDTO dto) {
        return stockService.freeze(dto);
    }
    public void order2(OrderingDTO dto) {
        orderMasterDao.insert(dto);
        int i=7/0;
        dto.getProducts().forEach(p->{
            orderItemDao.insert(p);
        });
    }

}
